package myJs.projectCps.pathwayAnaTab

import cats.effect._
import com.softwaremill.quicklens._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react._
import japgolly.scalajs.react.util.EffectCatsEffect._
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.Utils.layer
import myJs.api.Api
import myJs.myPkg.Hooks._
import myJs.myPkg.Swal.swal
import myJs.myPkg._
import myJs.myPkg.reactHookForm.{FormProvider, ReactHookForm, UseFormOptions, yupResolver}
import myJs.myPkg.tabs._
import myJs.projectCps._
import myJs.tool.Tool
import myJs.tool.Tool._
import myJs.user.FeatureSelectionTool
import shared.pojo.MissionTool.CheckFileExist
import shared.pojo.mission
import shared.pojo.mission._
import shared.tool.DefaultPickle
import upickle.default._

import scala.collection.immutable.SeqMap
import scala.scalajs.js
import scala.scalajs.js.{Date, JSON}
import cats.syntax.all._

/** Created by yz on 3/11/2020
  */
object Cp {

  case class Props(idStr: String = "") {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  case class TabInfo(key: String, label: String, pngFileName: String, cp: () => VdomElement)

  val Component = ScalaFnComponent[Props] { props =>
    val tabInfos = List(
      TabInfo(
        EnrichBarPlot.name,
        messages("enrichmentBarplot"),
        EnrichBarPlot.pngFileNamePrefixIn,
        () => enrichBarPlot.Cp.Props(idStr = props.idStr).render
      ),
      TabInfo(
        EnrichNetworkPlot.name,
        messages("network"),
        EnrichNetworkPlot.pngFileNamePrefixIn,
        () => enrichNetworkPlot.Cp.Props(idStr = props.idStr).render
      ),
      TabInfo(
        PathwayBubblePlot.name,
        messages("pathwayBubbleChart"),
        PathwayBubblePlot.pngFileNamePrefixIn,
        () => pathwayBubblePlot.Cp.Props(idStr = props.idStr).render
      ),
      TabInfo(
        PathwayBarPlot.name,
        messages("pathwayDiagram"),
        PathwayBarPlot.pngFileNamePrefixIn,
        () => pathwayBarPlot.Cp.Props(idStr = props.idStr).render
      )
    )
    val tabs = tabInfos.map(_.key)
    val initTab = tabs(0)
    val initTabExistMap = tabs.map(x => (x, false)).toSeqMap
    val (tabExistMap, setTabExistMap) = useState(initTabExistMap)
    val (tab, setTab) = useState(initTab)

    val idStr = props.idStr

    object FTool {

      def tabChange = (v: String) => {
        setTab(v)
      }

      def refreshTabExist(tabName: String, fileNamePrefixIn: String) = {
        val fileName = s"${fileNamePrefixIn}.png"
        val finalFileName = FeatureSelectionTool.getFinalFileName(idStr, fileName)
        val request = CheckFileExist.Request(missionId = Tool.getMissionId, fileName = finalFileName)
        Api.missionTool.checkFileExist(request).flatMapSync { b =>
          setTabExistMap { map =>
            map.updated(tabName, b)
          }
        }
      }

    }

    useEffect(
      {
        tabInfos.parTraverse { tabInfo =>
          FTool.refreshTabExist(tabInfo.key, tabInfo.pngFileName)
        }.void
      },
      List(props.idStr)
    )

    useEffect(
      {
        val curTab = tabExistMap.find(_._2).map(_._1).getOrElse(tabs.head)
        setTab(curTab)
      },
      List(tabExistMap)
    )

    if (tabExistMap.exists(_._2)) {
      Tabs(id = "tab", activeKey = tab, onSelect = FTool.tabChange, transition = false)(
        tabInfos.toVdomArray { tabInfo =>
          if (tabExistMap(tabInfo.key)) {
            Tab(eventKey = tabInfo.key, title = tabInfo.label, key = tabInfo.key)(
              div(
                marginTop := "20px",
                tabInfo.cp()
              )
            )
          } else EmptyVdom
        }
      )
    } else {
      div(marginLeft := 15.px, color.red, "No pathway analysis result!")
    }

  }

}
